package com.miaoxie.forum.module.monitor.server.controller;

import com.miaoxie.forum.module.auth.shared.controller.ResponseMessageController;
import com.miaoxie.forum.module.auth.shared.json.JSON;
import com.miaoxie.forum.module.auth.shared.security.SecurityNonCheckRequired;
import com.miaoxie.forum.module.monitor.server.services.IMonitorOfBiService;
import com.miaoxie.forum.module.monitor.shared.entity.ZLjobEntity;
import com.miaoxie.forum.module.monitor.shared.util.DebugRunnable;
import com.miaoxie.forum.module.monitor.shared.util.XmlIo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by miaoxie on 2017/2/21.
 * 移动BI访问人数、次数Contorller
 */
@Controller
@RequestMapping("/monitor/bi")
@SecurityNonCheckRequired
public class MonitorOfBIController extends ResponseMessageController {
    @Autowired
    private IMonitorOfBiService monitorOfBiService;

    public static Integer pageNo = 0;
    public static String parm = "";


    /**
     *获取智联信息（待使用）
     * @return
     * @throws ParseException
     */
    @RequestMapping("/getZl")
    @ResponseBody
    @JSON
    public Map<String, Object> getZl(String gz,String xl,String zw) throws ParseException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        List<Future<List<ZLjobEntity>>> resultList = new ArrayList<Future<List<ZLjobEntity>>>();
        String url = "http://sou.zhaopin.com/jobs/searchresult.ashx";
        String parm = "jl=上海&kw="+zw+"&sf="+gz+"&el="+xl;
        Integer pageNo = 0;


        for (int i = 0; i < 2; i++){
            //使用ExecutorService执行Callable类型的任务，并将结果保存在future变量中
            pageNo+=1;
            String pagesParm = parm+"&p="+pageNo;
            Future<List<ZLjobEntity>> future = executorService.submit(new DebugRunnable(url,pagesParm));
            //将任务执行结果存储到List中
            resultList.add(future);
        }

        List<ZLjobEntity> result = new ArrayList<>();
        for (Future<List<ZLjobEntity>> fs : resultList){
            try{
                while(!fs.isDone());//Future返回如果没有完成，则一直循环等待，直到Future返回完成
                List<ZLjobEntity> list = fs.get();
                if(list!=null && list.size()!=0)
                    result.addAll(list);
            }catch(InterruptedException e){
                e.printStackTrace();
            }catch(ExecutionException e){
                e.printStackTrace();
            }finally{
                //启动一次顺序关闭，执行以前提交的任务，但不接受新任务
                executorService.shutdown();
            }
        }

//        List<ZLjobEntity> result = jobList(pageNo);
        this.getMap().put("result", result);
        return this.returnSuccess();
    }
    @RequestMapping("/getbdwk")
    @ResponseBody
    @JSON
    public Map<String, Object> getbdwk(String gz,String xl,String zw) throws ParseException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        List<Future<List<ZLjobEntity>>> resultList = new ArrayList<Future<List<ZLjobEntity>>>();
        String url = "http://sou.zhaopin.com/jobs/searchresult.ashx";
        return returnSuccess();
    }















    /**
     * 移动Vp监控界面URL前缀（待使用）
     * @return
     * @throws ParseException
     */
    @RequestMapping("/getVpVMUrl")
    @ResponseBody
    @JSON
    public Map<String, Object> getVpVMUrl() throws ParseException {
        String url = XmlIo.readVPURL();
        this.getMap().put("url", url);
        return this.returnSuccess();
    }

    /**
     * 配置移动Vp监控界面URL前缀（待使用）
     * @param files
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/upload", method= RequestMethod.POST)
    public String showUploadPage(@RequestParam("file")MultipartFile[] files,
                                 HttpServletRequest request, ModelMap model){
        if(files!=null&&files.length>0){
            for(int i=0;i<files.length;i++){
                MultipartFile file = files[i];
                //调用储存file的函数
                monitorOfBiService.saveFile(request, file);
            }
        }
        return "uploadFile";         //view文件夹下的上传单个文件的页面
    }

}
